﻿//toast(info)   new toast(info)
//运用单例模式
define([], function() {
	return(function() {
		var t;
		return function(info) {
			if(!t) {
				t = document.createElement("div");
				with(t.style) {
					padding = "15px";
					backgroundColor = "black";
					color = "white";
					fontSize = "15px";
					fontFamily = "微软雅黑";
					borderRadius = "5px";
					position = "fixed";
					opacity = 0;
					left = "50%";
					top = "50%";
				}
				document.body.appendChild(t);
				t.style.marginLeft = -t.offsetWidth / 2 + "px";
				t.style.marginTop = -t.offsetHeight / 2 + "px";
				t.show = function(callback) {
					clearInterval(t.timer);
					var num = 0;
					t.timer = setInterval(function() {
						t.style.opacity = (num += 0.02);
						if(num >= 1) {
							clearInterval(t.timer);
							setTimeout(function() {
								callback();
							}, 1000);
						}
					}, 30);
				};
				t.hide = function() {
					clearInterval(t.timer);
					var num = 1;
					t.timer = setInterval(function() {
						t.style.opacity = (num -= 0.02);
						if(num <= 0) {
							clearInterval(t.timer);
						}
					}, 30);
				};
			}
			t.innerText = info;
			new Promise(t.show).then(t.hide);
		}
	})();
})

/*function toast(info){
	var t = document.getElementById("toast");
	if( t ) {
		t.innerText = info;
		t.style.display = "block";
		t.timer = setInterval(function(){
			
		},)
		setTimeout(function(){
			var num = 1;
			t.timer = setInterval(function(){
				t.style.opacity = (num-=0.02);
				if(num == 0) clearInterval(t.timer);
			},30);
		},2000);
		return;
	} else {
		var div = document.createElement("div");
		div.id = "toast";
		with(div.style) {
			padding = "15px";
			backgroundColor = "black";
			color = "white";
			fontSize = "15px";
			fontFamily = "微软雅黑";
			borderRadius = "5px";
			position = "fixed";
			opacity = 0;
			left = "50%";
			top = "50%";	
		}
		div.innerText = info;
		document.body.appendChild(div);
		div.style.marginLeft = -div.offsetWidth/2 + "px";
		div.style.marginTop = -div.offsetHeight/2 + "px";
		
		setTimeout(function(){
			var num = 1;
			div.timer = setInterval(function(){
				div.style.opacity = (num-=0.02);
				if(num == 0) clearInterval(div.timer);
			},30);
		},2000);
	}
	
}*/